package com.ByteDance.controller;


import com.ByteDance.model.po.CommentEntity;
import com.ByteDance.model.po.UserInfos;
import com.ByteDance.model.vo.Comment;
import com.ByteDance.model.vo.CommentVO;
import com.ByteDance.model.vo.User;
import com.ByteDance.service.ICommentService;
import com.ByteDance.service.IUserService;
import com.ByteDance.service.IVideoService;
import com.ByteDance.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * 视频评论表 前端控制器
 * </p>
 *
 * @author 朱哥
 * @since 2024-03-04
 */
@Slf4j
@Api(tags = "CommentController")
@RestController
@RequestMapping("douyin/comment")
@RequiredArgsConstructor
public class CommentController {
    private final IUserService iUserService;
    private final IVideoService iVideoService;
    private final ICommentService iCommentService;

    @ApiOperation("评论操作接口")
    @PostMapping("/action")
    public CommentVO action(@RequestParam(value = "video_id")int videoId,
                            @RequestParam(value = "action_type")int actionType,
                            @RequestParam(value = "comment_text",required = false)String commentText,
                            @RequestParam(value = "comment_Id" , required = false)Integer commentId,
                            HttpServletRequest req){

        if(iVideoService.getById(videoId) == null){//视频不存在
            return CommentVO.fail();
        }

        int userId = JwtUtils.getUserId(req.getHeader("token"));
        UserInfos userInfos = iUserService.getById(userId);
        User user = new User();
        user.setUserId(userId);
        user.setUsername(userInfos.getUsername());
        user.setFollowCount(userInfos.getFollowCount());
        user.setFollowerCount(userInfos.getFollowerCount());
        user.setFollow(false);
        user.setAvatar(userInfos.getAvatar());
        user.setBackgroundImage(userInfos.getBackgroundImage());
        user.setSignature(userInfos.getSignature());
        user.setTotalFavorited(userInfos.getTotalFavorited());
        user.setWorkCount(userInfos.getWorkCount());
        user.setFavoriteCount(userInfos.getFavoriteCount());


        CommentEntity commentEntity = iCommentService.PostComment(userId, videoId, actionType, commentText, commentId);
        if(commentEntity == null){//评论操作失败
            return CommentVO.fail();
        }
        //评论成功，构建CommentVO
        CommentVO commentVO = CommentVO.success();
          //构建comment
        Comment comment = new Comment();
        comment.setId(commentEntity.getCommentId());
        comment.setUser(user);
        comment.setContent(commentEntity.getCommentText());
        String createDate = formatCreateDate(commentEntity.getCreateDate());
        comment.setCreateDate(createDate);
        commentVO.setComment(comment);
        log.info("视频评论成功：" + comment);
        return commentVO;
    }

    @ApiOperation("查看视频评论列表接口")
    @GetMapping("/list")
    public CommentVO list(@RequestParam(value = "video_id")int videoId,HttpServletRequest req){
        if(iVideoService.getById(videoId) == null){//视频不存在
            return CommentVO.fail();
        }
        int yid = JwtUtils.getUserId(req.getHeader("token"));
        Comment[] comments = iCommentService.searchCommentByVideoId(yid, videoId);
        CommentVO commentVO = CommentVO.success();
        commentVO.setCommentList(comments);
        return commentVO;
    }

    public String formatCreateDate(LocalDate localDate) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");
        return localDate.format(formatter);
    }


}
